﻿using Journey.Collections;

namespace Journey.Adapter
{
    using Journey.Domain;
    using System.Collections.Generic;

    /// <summary>
    /// 自动类型自动映射适配器扩展。
    /// </summary>
    public static class ProjectionsExtensions
    {
        /// <summary>
        /// 实体对象映射。
        /// </summary>
        /// <typeparam name="TProjection">目标类型。</typeparam>
        /// <param name="item">实体对象。</param>
        /// <returns>目标对象。</returns>
        public static TProjection ProjectedAs<TProjection>(this IEntity item)
            where TProjection : class, new()
        {
            var adapter = TypeAdapterFactory.CreateAdapter();
            return adapter.Adapt<TProjection>(item);
        }

        /// <summary>
        /// 实体对象集合映射。
        /// </summary>
        /// <typeparam name="TProjection">目标类型。</typeparam>
        /// <param name="items">实体对象集合。</param>
        /// <returns>目标对象集合。</returns>
        public static List<TProjection> ProjectedAsCollection<TProjection>(this IEnumerable<IEntity> items)
            where TProjection : class, new()
        {
            var adapter = TypeAdapterFactory.CreateAdapter();
            return adapter.Adapt<List<TProjection>>(items);
        }

        /// <summary>
        /// 实体对象集合映射。
        /// </summary>
        /// <typeparam name="TProjection">目标类型。</typeparam>
        /// <param name="items">实体对象集合。</param>
        /// <returns>目标对象集合。</returns>
        public static IPageOfItems<TProjection> ProjectedAsPageOfCollection<TProjection>(this IPageOfItems<IEntity> items)
            where TProjection : class, new()
        {
            var adapter = TypeAdapterFactory.CreateAdapter();
            var pageOfItems = adapter.Adapt<PageOfItems<TProjection>>(items);

            pageOfItems.PageSize = items.PageSize;
            pageOfItems.TotalItemCount = items.TotalItemCount;
            pageOfItems.PageIndex = items.PageIndex;

            return pageOfItems;
        }
    }
}